package com.F02.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.F02.action.F020204Action;
import com.F02.actionform.F020204ActionForm;
import com.F02.dao.F020204DAO;
import com.action.BaseAction;
import com.config.Constants;
import com.exception.SelectException;
import com.info.T_S_YHCZXXinfo;
import com.util.CommonTools;
import com.util.SessionUtils;

/**
 * @leip
 * @非标准产品BOMList初始化
 * @功能：初始化，查询
 * @规范：execute()方法制作判断转发，不处理具体业务�? @ ACTION中所有�?�辑判断对应的具体业务方法的命名规则
 * do+静态参数，例如:operationType=init，实现方法为doINIT(). @ 涉及到连接数据库的方法全部写在DAO中�
 * @调试信息打印使用LOG.INFO();
 * @记得加注释

 */

public class F020204Action extends BaseAction{
	/** 记录操作日志 */
	Logger log = Logger.getLogger(F020204Action.class);

	/** 操作记录INFO */
	T_S_YHCZXXinfo t_YhczxxInfo = null;		
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		/** 获得actionForm对象 */
		F020204ActionForm actionForm = (F020204ActionForm) form;		
		/** 获得operationType值�?�做执行判断�? */
		String operationType = actionForm.getOperationType();
		log.info("operationType = " + operationType);
		/** 获取实例 */
		F020204DAO dao = new F020204DAO();
		/**页面语言选择提示*/
		String language = SessionUtils.getLocale(request);
		actionForm.setLanguage(language);
		try {
			/** 生成操作记录语句 */
			t_YhczxxInfo = setYhczxx(request, response, operationType,
					"F020203");
			/**
			 * @页面初始�?
			 * @统一参数：operationType=init
			 * @规范：左树进入连接页面不执行查询�?
			 */
			if (Constants.INIT.equals(operationType)) {
				this.doINIT(actionForm,dao);
				return mapping.findForward("bom");
			}
			/**
			 * @查询功能
			 * @统一参数：operationType=queryList
			 * @规范：查询后回显查询条件。如果查询结果无记录，在页面给出提示�?
			 */
			if ("bomQuery".equals(operationType)) {
				this.doQUERYBOMLIST(actionForm, dao, request);
				return mapping.findForward("bom");
			}
			/**
			 * @查询功能
			 * @统一参数：operationType=queryList
			 * @规范：查询后回显查询条件。如果查询结果无记录，在页面给出提示�?
			 */
			if ("bom".equals(operationType)) {
				this.doQUERYLIST(actionForm, dao, request);
				return mapping.findForward("bom");
			}
			/**
			 * @刷新功能
			 * @统一参数：operationType=refresh
			 * @规范：查询后回显查询条件。如果查询结果无记录，在页面给出提示�?
			 */
			if ("refresh".equals(operationType)) {
				this.doREFRESHLIST(actionForm, dao, request);
				return mapping.findForward("bom");
			}
			/**
			 * @刷新翻页功能
			 * @统一参数：operationType=queryList
			 * @规范：查询后回显查询条件。如果查询结果无记录，在页面给出提示�?
			 */
			if (Constants.QUERYLIST.equals(operationType)) {
				this.doREFRESHLIST(actionForm, dao, request);
				return mapping.findForward("bom");
			}
			/**
			 * @页面AJAX功能
			 * @统一参数：operationType=ajax
			 * @规范：在本页面动态加载值
			 */
			if (Constants.AJAX.equals(operationType)) {
				this.initAjax(actionForm,dao,request,response);
				return null;
			}
			/**
			 * @删除功能
			 * @统一参数：operationType=delete
			 * @规范：修改保存后，返回查询页面，更新记录，给出用户提示信息�??
			 */
			if (Constants.DELETE.equals(operationType)) {
				this.doDELETE(actionForm, dao, request);
				return mapping.findForward("bom");
			}
			/**
			 * @新增保存功能
			 * @统一参数：operationType=update
			 * @规范：修改保存后，返回查询页面，更新记录，给出用户提示信息�??
			 */
			if (Constants.SAVE_UPDATE.equals(operationType)) {
				this.doSAVE_INSERT(actionForm, dao, request);
				return mapping.findForward("bom");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (dao != null)
				dao.disConnect();
		}
		return mapping.findForward(Constants.INIT);
	}

	/**
	 * @初始�?
	 */
	public void doINIT(F020204ActionForm actionForm,F020204DAO dao
			) throws SelectException {
		actionForm.setPhasesCode("");
	}

	/**
	 * @查询
	 */
	private void doQUERYLIST(F020204ActionForm actionForm, F020204DAO dao,
			HttpServletRequest request) throws SelectException {
			actionForm.setPhasesList(dao.List_t_critical_phases(request.getParameter("productType")));
			List list = new ArrayList();
			doClearForm(actionForm);
			actionForm.setCode(request.getParameter("code"));
			actionForm.setPhasesCode("");
			list = dao.QUERYLIST(actionForm, request);
			actionForm.setList(list);
			if (list != null && list.size() > 0) {
				actionForm.setList(list);
			} else {				
				actionForm.setErrorFlag("2");
			}
			log.info(""+actionForm.getList().size());
			actionForm.setOperationType(Constants.SAVE_UPDATE);
	}
	/**
	 * @查询
	 */
	private void doQUERYBOMLIST(F020204ActionForm actionForm, F020204DAO dao,
			HttpServletRequest request) throws SelectException {			
			List list = new ArrayList();
			doClearForm(actionForm);
			actionForm.setManufacturingOrder(CommonTools.null2String(request.getParameter("manu")));
			actionForm.setProductCode(CommonTools.null2String(request.getParameter("productCode")));
			actionForm.setFamilyCode(CommonTools.null2String(request.getParameter("familyCode")));
			actionForm.setProductType(CommonTools.null2String(request.getParameter("productType")));
			actionForm.setFromTable(CommonTools.null2String(request.getParameter("fromTable")));
			list = dao.QUERYBOMLIST(actionForm, request);			
			if (list != null && list.size() > 0) {
				actionForm.setList(list);
			} else {
				actionForm.setList(list);
				actionForm.setErrorFlag("2");
			}			
			actionForm.setOperationType(Constants.SAVE_UPDATE);
	}
	/**
	 * @刷新
	 */
	private void doREFRESHLIST(F020204ActionForm actionForm, F020204DAO dao,
			HttpServletRequest request) throws SelectException {					
			List list = new ArrayList();
			doClearForm(actionForm);
			list = dao.REFRESHLIST(actionForm, request);			
			if (list != null && list.size() > 0) {
				actionForm.setList(list);
			} else {
				actionForm.setList(list);
				actionForm.setErrorFlag("2");
			}			
			actionForm.setOperationType(Constants.SAVE_UPDATE);
	}
	/**
	 * @throws SelectException
	 * @执行页面动态加载
	 */
	private void initAjax(F020204ActionForm actionForm,F020204DAO dao,
			HttpServletRequest request, HttpServletResponse response){
				
		try{
			response.setContentType("text/xml");
			response.setCharacterEncoding("UTF-8");
			
			String id = CommonTools.trim(request.getParameter("id"));
			if("update".equals(id)){
				String code = CommonTools.trim(request.getParameter("code"));
				String rowId = CommonTools.trim(request.getParameter("rowId"));
				String quantity = CommonTools.trim(request.getParameter("quantity"));
				//
				actionForm.setCode(code);
				actionForm.setId(rowId);
				actionForm.setQuantity(quantity);
				/** EXT AJAX */
				StringBuffer sb = new StringBuffer();
				String bs = "1";
				sb.append("{success:true, data:{");
								
				if (dao.SAVE_UPDATE(actionForm,t_YhczxxInfo)) {
					dao.UPDATE_COST(actionForm);
					bs = "0";
				}

				sb.append("value1:").append(bs);
				response.getWriter().write(
				sb.substring(0, sb.length()).concat("}}"));
				log.info(sb.substring(0, sb.length()).concat("}}"));
			}else if("comboBox".equals(id)){
				String code = CommonTools.trim(request.getParameter("code"));
				String name = CommonTools.trim(request.getParameter("name"));
				if("materialCode".equals(code)){
					this.setFilterList(dao.filterList("t_materials", "code", "name", "code like '%" + name + "%'"), response);
				}else if("materialName".equals(code)){
					this.setFilterList(dao.filterList("t_materials", "code", "name", "name like '%" + name + "%'"), response);
				}
			}			
									
		}catch (Exception e){
			e.printStackTrace();
		}
	}
	/**
	 * @删除
	 */
	private void doDELETE(F020204ActionForm actionForm, F020204DAO dao,
			HttpServletRequest request) throws SelectException {
		dao.DELETE(actionForm, t_YhczxxInfo, request);
		dao.UPDATE_COST(actionForm);
		this.doREFRESHLIST(actionForm, dao, request);
	}
	/**
	 * @新增保存
	 * @执行新增后，清空actionForm，执行查询操作，返回查询界面。

	 */
	private void doSAVE_INSERT(F020204ActionForm actionForm, F020204DAO dao,
			HttpServletRequest request) throws SelectException {
		dao.SAVE_INSERT(actionForm, t_YhczxxInfo);
		dao.UPDATE_COST(actionForm);
		this.doActionForm(actionForm, dao, request);		
	}
	/**
	 * @throws SelectException
	 * @清空actionForm
	 * @执行查询回显操作
	 */
	private void doActionForm(F020204ActionForm actionForm, F020204DAO dao,
			HttpServletRequest request) throws SelectException {						
		//清空输入值			
		doClearForm(actionForm);
		this.doREFRESHLIST(actionForm, dao, request);
	}
	/**	 
	 * @清空actionForm
	 * @
	 */
	private void doClearForm(F020204ActionForm actionForm){						
		//清空输入值						
		actionForm.setOperationType("");
		//actionForm.setPhasesCode("");
		actionForm.setMaterialCode("");
		actionForm.setMaterialName("");
		actionForm.setDescription("");
		actionForm.setQuantity("");
		actionForm.setErrorFlag("");
	}
}
